package cn.tedu.medicalmanagementsystem.manager.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeDelParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ServiceTypeVO;
import cn.tedu.medicalmanagementsystem.manager.service.ServiceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/serviceType")
@Slf4j
@Api(tags = "预约服务类型模块")
public class ServiceTypeController {

    @Autowired
    ServiceTypeService serviceTypeService;

    @PostMapping("/add")
    @ApiOperation("添加服务类型")
    public JsonResult add(ServiceTypeAddParam serviceTypeAddParam) {
        log.debug("【添加服务类型】请求参数: {}", serviceTypeAddParam);
        ServiceTypeVO vo = serviceTypeService.add(serviceTypeAddParam);
        return JsonResult.ok(vo);
    }

    @PostMapping("/update")
    @ApiOperation("更新服务类型")
    public JsonResult update(ServiceTypeUpdateParam serviceTypeUpdateParam) {
        log.debug("【更新服务类型】请求参数: {}", serviceTypeUpdateParam);
        ServiceTypeVO vo = serviceTypeService.update(serviceTypeUpdateParam);
        return JsonResult.ok(vo);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除服务类型")
    public JsonResult delete(ServiceTypeDelParam serviceTypeDelParam) {
        log.debug("【删除服务类型】请求参数: {}", serviceTypeDelParam);

        serviceTypeService.delete(serviceTypeDelParam);
        return JsonResult.ok();
    }

    @GetMapping("/types")
    @ApiOperation("查询服务类型列表")
    public JsonResult list() {
        log.debug("【查询服务类型列表】");
        List<ServiceTypeVO> list = serviceTypeService.allTypes();
        return JsonResult.ok(list);
    }

    @GetMapping("/select")
    public JsonResult selectServiceType(ServiceTypeListQuery serviceTypeListQuery){
        log.debug("开始处理查询服务类型列表业务，参数: {}", serviceTypeListQuery);
        List<ServiceTypeVO> list = serviceTypeService.selectServiceType(serviceTypeListQuery);
        return JsonResult.ok(list);
    }



    @GetMapping("/{id}")
    @ApiOperation("根据服务类型ID查询服务类型")
    public JsonResult getById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询服务类型】的请求，参数：{}", id);

        ServiceTypeVO serviceTypeVO = serviceTypeService.selectById(id);
        return JsonResult.ok(serviceTypeVO);
    }

    @GetMapping("/name")
    @ApiOperation("根据服务类型名称查询服务类型")
    public JsonResult getByName(@RequestParam(required = false, defaultValue = "") String name) {
        log.debug("开始处理【根据名称查询服务类型】的请求，参数：{}", name);
        if (name.isEmpty()) {
            return JsonResult.fail("服务类型名称不能为空");
        }
        ServiceTypeVO serviceTypeVO = serviceTypeService.selectByName(name);
        return serviceTypeVO != null ? JsonResult.ok(serviceTypeVO) : JsonResult.fail("未找到该服务类型");
    }


}
